﻿using Domain.InvoiceTicket.Invoicing;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.InvoiceTicket.Invoice;
using MES.Server.API.Dto.InvoiceTicketDTO;
using ReturnValue;

namespace MES.Server.API.Application.Handler.InvoiceTicket.Invoice
{
    public class GetInvoicingHandler : IRequestHandler<GetInvoicingCommand, ApiPaging<GetInvoicingDTO>>
    {
        private readonly IRepository<InvoicingModel> _invoicingRepo;

        public GetInvoicingHandler(IRepository<InvoicingModel> invoicingRepo)
        {
            _invoicingRepo = invoicingRepo;
        }

        public Task<ApiPaging<GetInvoicingDTO>> Handle(GetInvoicingCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiPaging<GetInvoicingDTO>();
            var ins = _invoicingRepo.GetAll().Where(x => !x.IsDel);

            var linq = from a in ins
                       select new GetInvoicingDTO()
                       {
                           Id= a.Id,
                           InvoicingCode= a.InvoicingCode,
                           InvoicingName= a.InvoicingName,
                           InvoicingType= a.InvoicingType,
                           InvoicingDate= a.InvoicingDate,
                           InvoicingMomeny= a.InvoicingMomeny,
                           ReceiveInvoicing=a.ReceiveInvoicing,
                           InvoicingDesc= a.InvoicingDesc
                       };
            res.Totalcount = linq.Count();
            res.TotalPage = linq.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            res.Message = "查询成功";
            res.Code=StatusCode.Success;
            return Task.FromResult(res);
        }
    }
}
